package com.group7.mall.service.impl;

import com.group7.mall.convert.CartConverter;
import com.group7.mall.dao.*;
import com.group7.mall.dto.UserCartPreviewDto;
import com.group7.mall.entity.TbGoods;
import com.group7.mall.entity.TbGoodsInventoryPrice;
import com.group7.mall.entity.TbUserCart;
import com.group7.mall.dto.UserCartAddDto;
import com.group7.mall.service.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    private TbUserCartDao tbUserCartDao;

    @Resource
    private TbGoodsInventoryPriceDao tbGoodsInventoryPriceDao;

    @Resource
    private TbGoodsDao tbGoodsDao;

    @Resource
    private TbGoodsTypeDao tbGoodsTypeDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Override
    public Integer queryAllCartNumber(String userOpenId) {
        return tbUserCartDao.queryAllCartNumber(userOpenId);
    }

    @Override
    public int insert(UserCartAddDto userCartAddDto) {
        TbGoodsInventoryPrice tbGoodsInventoryPrice = tbGoodsInventoryPriceDao.queryTbGoodsInventoryPriceByTypesByTypeIds(userCartAddDto);
        TbUserCart tbUserCart = new TbUserCart();
        tbUserCart.setUserOpenId(userCartAddDto.getUserOpenId());
        tbUserCart.setCombinationId(tbGoodsInventoryPrice.getCombinationId());
        tbUserCart.setGoodsId(tbGoodsInventoryPrice.getGoodsId());
        if (tbUserCartDao.count(tbUserCart) > 0) {
            TbUserCart tbUserCart1 = tbUserCartDao.queryByUserOpenIdAndCombinationId(userCartAddDto.getUserOpenId(), tbGoodsInventoryPrice.getCombinationId());
            tbUserCart1.setCartNumber(tbUserCart1.getCartNumber() + userCartAddDto.getCount());
            tbUserCart1.setUpdatedTime(userCartAddDto.getUpdatedTime());
            return tbUserCartDao.update(tbUserCart1);
        } else {
            tbUserCart.setCartNumber(userCartAddDto.getCount());
            if (tbGoodsInventoryPrice.getInventory() < 10 && tbGoodsInventoryPrice.getInventory() > 0) {
                tbUserCart.setIsInventoryShortage(1);
                tbUserCart.setIsInventoryInsufficient(0);
            } else if (tbGoodsInventoryPrice.getInventory() <= 0) {
                tbUserCart.setIsInventoryShortage(1);
                tbUserCart.setIsInventoryInsufficient(1);
            } else {
                tbUserCart.setIsInventoryShortage(0);
                tbUserCart.setIsInventoryInsufficient(0);
            }
            TbGoods tbGoods = tbGoodsDao.queryById(userCartAddDto.getGoodsId());
            tbUserCart.setStoreId(tbGoods.getStoreId());
            tbUserCart.setIsInvalid(0);
            String typeName = tbGoodsTypeDao.queryById(userCartAddDto.getTypeId1()).getTypeName();
            if (userCartAddDto.getTypeId2() != null) {
                typeName += ";" + tbGoodsTypeDao.queryById(userCartAddDto.getTypeId2()).getTypeName();
            }
            if (userCartAddDto.getTypeId3() != null) {
                typeName += ";" + tbGoodsTypeDao.queryById(userCartAddDto.getTypeId3()).getTypeName();
            }
            tbUserCart.setTypeInfo(typeName);
            // TODO 设置更新时间（已完成）

            tbUserCart.setUpdatedTime(userCartAddDto.getUpdatedTime());
            tbUserCart.setCreatedTime(userCartAddDto.getCreatedTime());
//            tbUserCart.setUpdatedTime(new Date());
//            tbUserCart.setCreatedTime(new Date());
            System.out.println(tbUserCart.toString());
        }
        return tbUserCartDao.insert(tbUserCart);
    }

    @Override
    public List<UserCartPreviewDto> showUserCart(String userOpenId) {
        List<TbUserCart> tbUserCartList = tbUserCartDao.queryByUserOpenId(userOpenId);
        List<UserCartPreviewDto> userCartPreviewDtoList = CartConverter.INSTANCE.convertTbUserCartListToUserCartDtoList(tbUserCartList);
        userCartPreviewDtoList.forEach(userCartPreviewDto -> {
            userCartPreviewDto.setStoreName(tbSellerDao.queryById(userCartPreviewDto.getStoreId()).getStoreName());
            TbGoodsInventoryPrice tbGoodsInventoryPrice = tbGoodsInventoryPriceDao.queryById(userCartPreviewDto.getCombinationId());
            if (tbGoodsInventoryPrice != null) {
                userCartPreviewDto.setPrice(tbGoodsInventoryPrice.getPrice());
                userCartPreviewDto.setInventory(tbGoodsInventoryPrice.getInventory());
                userCartPreviewDto.setGoodsStatue(0);
            } else {
                userCartPreviewDto.setPrice(null);
                userCartPreviewDto.setInventory(null);
                userCartPreviewDto.setGoodsStatue(1);
            }
            TbGoods tbGoods = tbGoodsDao.queryById(userCartPreviewDto.getGoodsId());
            if (tbGoods.getIsDeleted() > 0) {
                userCartPreviewDto.setGoodsStatue(2);
            }
            userCartPreviewDto.setImage(tbGoods.getGoodsImage());
            userCartPreviewDto.setGoodsInfo(tbGoods.getGoodsInfo());
            userCartPreviewDto.setBrand(tbGoods.getBrand());
        });
        return userCartPreviewDtoList;
    }

    @Override
    public int deleteById(Integer cartId) {
        return tbUserCartDao.deleteById(cartId);
    }

    @Override
    public int updateCartNumberById(TbUserCart tbUserCart) {
        return tbUserCartDao.update(tbUserCart);
    }
}
